var uploadVideoDialog = function () {
    function UploadVideoDialogClass() {
    }
    UploadVideoDialogClass.prototype = {
        _last_type:1,//1"Upload"  2"Replace";
        _last_one_callback_func:null,//进入showUI时的 上传完毕回调函数
        getHiddenUI:function () {
            // return $(".replacepopup-wrap");
            return $(".replacepopup-wrap2");
        },

        getTopOffset:function ($obj) {
            //设置弹出窗口top值
            var top=($(window).height()-($obj.height()+70))/2;
            if(top<0){
                top=0;
            };
            return top;
        },
        closeUI:function () {
            var  $this = this;
            layer.closeAll();

            $this._last_type=0;
            $this._last_one_callback_func=null;
        },
        showUI:function (tp,upParam,cb) {
            var  $this = this;
            $this._last_type= tp;
            $this._last_one_callback_func= cb;

            // $this.getHiddenUI().show();
            var hiddenUI = $this.getHiddenUI();
            hiddenUI.removeClass("display-none");

            //create a new one
            var cl = $("#fromLocalBtnProto").clone();
            hiddenUI.children(".local-ico").remove();
            hiddenUI.prepend(cl);

            // uploadVideoDialog.getHiddenUI().find("object").remove();


            //bind upload
            try {
                var generatedId="xyzUploadLvId"+new Date().getTime();
                var $newDiv = $("<span></span>");
                $newDiv.attr("id",generatedId);
                // cl.attr("id",generatedId);
                cl.append($newDiv);

                // cl.click(function (e) {
                //     console.log('outer local click... ');
                //     // console.log(e.target);
                //     if($(e.target).hasClass("local-ico")){
                //         console.log('-----------------------------');
                //         console.log("trigger upload...");
                //
                //         //for !IE
                //         $newDiv.find("input").click();
                //
                //         //for IE
                //         cl.find("object").click();
                //         var objectEle = cl.find("object")[0];
                //         // console.log(objectEle);
                //         // console.log(JSON.stringify(cl));
                //         // console.log(cl.children());
                //         if(objectEle){
                //             console.log('ie. play object.');
                //             // objectEle.Play();
                //             objectEle.play();
                //
                //         }
                //         console.log('-----------------------------');
                //     } else {
                //         console.log('it\'s  propagation,just ignore');
                //     }
                //
                // });

                upParam.targetId=generatedId;
                upParam.buttonWidth ="200";
                upParam.buttonHeight ="50";// IE789下设置此按钮高度生效
                upParam.buttonText=zLocale.thirdstep_fromlocal;
                // upParam.buttonText="<div class='aaa' style='font-size: 30px;'>"+zLocale.thirdstep_fromlocal+"</div>";
                // upParam.buttonText=zLocale.thirdstep_fromlocal;
                // upParam.buttonClass="local-ico";
                upParam.buttonTextStyle="{  font-size: 18;color:#ffffff;text-align:center;line-height:50px;font-family:\"Microsoft Yahei\",arial,����,\"Helvetica Neue\",Helvetica,STHeiTi,sans-serif}",
                upParam.buttonTextTopPadding =12;

                // var realLogic=function () {
                // cl.Ableuploader(upParam);
                $newDiv.Ableuploader(upParam);
                // }
                //
                // if($.isIE){
                //     setTimeout(realLogic, 0);
                // }else{
                //     realLogic();
                // }
            }catch (e){
                //console.log(e);
            }

            var title  = tp==1?zLocale.database_shangchuan:zLocale.thirdstep_replace;

            //弹出窗口
            layer.open({
                offset:[$this.getTopOffset(hiddenUI)+"px"],
                type:1,
                shade: 0.6,
                title :title,
                area:["740px","auto"],
                fix: false, //不固定
                content: hiddenUI
            });



        }

    }

    return new UploadVideoDialogClass();
}();

var uploadListDialog = function () {
    function UploadListDialog() {
    }
    UploadListDialog.prototype = {
        getListUI:function () {
            return $(".librarypopup-div");
        },
        showNoDataDiv:function (bl) {
            var  $this = this;
            var listUI = $this.getListUI();
            var $target = listUI.find(".nocontent");
            bl?$target.show():$target.hide();
        },
        showDataDiv:function (bl) {
            var  $this = this;
            var listUI = $this.getListUI();
            var $target = listUI.find(".contentexists").add(listUI.find(".btnlist"));
            bl?$target.show():$target.hide();
        },
        getSelectedTr:function () {
            var  $this = this;
            var listUI = $this.getListUI();
            switch (uploadVideoDialog._last_type) {
                case 1:return listUI .find("input:checkbox:checked").parents("tr");
                case 2:return listUI .find("input:radio:checked").parents("tr");
            }
        },
        getSelectedDataArr:function () {
            var  $this = this;
            var $arr = $this.getSelectedTr();
            var ret = [];
            $arr.each(function (i, e) {
                ret.push($(e).data("_data"));
            });
            return ret;
        },
        checkSelected:function () {
            var  $this = this;
            var $arr = $this.getSelectedTr();
            var ret  = !!$arr.length;

            return ret;
        },
        paintListUI:function () {
            var  $this = this;
            var tp = uploadVideoDialog._last_type;

            //弹出窗口
            var listUI = $this.getListUI();

            // .filter(":visible")
            $this.showNoDataDiv(false);
            $this.showDataDiv(false);

            var $tbody = listUI.find("tbody");
            $tbody.empty();

            var $proto ;

            switch (tp) {
                case 1:$proto= $("#uploadVideoListProto").clone().removeAttr("id");break;
                case 2:$proto= $("#replaceVideoListProto").clone().removeAttr("id");break;
            }

            $.ajax({
                url: basePath + "/course/thirdStep/file/videos",
                data: {courseId:$("#courseId").val()},
                async: false,
                type:"POST",
                success: function (json) {
                    // console.log(json);
                    if(isRequestSuccess(json)){
                        var arr = json.result;
                        console.log(arr);

                        var ic = 1;
                        arr.map(function (data) {
                            var $cl = $proto.clone();

                            if ((ic%2)==0) {
                                $cl.addClass("librarytr-even");
                            }
                            ic++;

                            // console.log($cl);
                            $cl.data("_data",data);
                            $cl.find(".filenamecontent").text(data.fileName);
                            if(data.size) {
                                $cl.find(".filenamesize").text(data.readableSize+""+data.readableUnit);
                            }
                            $tbody.append($cl);
                        });

                        arr.length>0?$this.showDataDiv(true):$this.showNoDataDiv(true);
                    }

                }
            });
        },
        showListUI:function () {
            var  $this = this;

            layer.closeAll();

            $this.paintListUI();

            //弹出窗口
            var listUI = $this.getListUI();

            layer.open({
                offset:[uploadVideoDialog.getTopOffset(listUI)+"px"],
                type:1,
                shade: 0.6,
                // shade: false,
                title :zLocale.thirdstep_library_choose_title,
                area:["950px","auto"],
                fix: false, //不固定
                content: listUI
            });

        }
    }

    return new UploadListDialog();
}();

//刷新打开页的视频列表
var _listenMovieNumCallBack = function () {
    //如果当前窗口已打开才查询
    var listUI = uploadListDialog.getListUI().filter(":visible");

    if(listUI.length){
        console.log('refreshed video list');
        uploadListDialog.paintListUI();
    }
}

$(function () {
    //From local

    //From Library
    uploadVideoDialog.getHiddenUI().find("a.libraryico").click(function () {
        console.log('Library');

        uploadListDialog.showListUI();
    });

    //Cancel
    uploadVideoDialog.getHiddenUI().find("a.cancel").click(function () {
        layer.closeAll();
    });

    //list-save
    uploadListDialog.getListUI().find(".UploadDialogSave").click(function () {
        console.log('save');

        if(uploadListDialog.checkSelected()){
            console.log('continue xhr...');

            var arr = uploadListDialog .getSelectedDataArr();
            var func = uploadVideoDialog._last_one_callback_func;

            arr.map(function (dt) {
                var p = {data:dt};

                func(p,2,dt);//可能已经转码好 需要传第3个参数
            });
            // console.log(arr);

            layer.closeAll();
        }else{
            console.log('select first');
            _alertManager.alertChooseOne();
        }

    });
    //list-Cancel
    uploadListDialog.getListUI().find(".UploadDialogCancel").click(function () {
        console.log('cancel');
        layer.closeAll();
    });


    //_debug模式开启时
    //press 1=进入资料页
    //press 2=点击第一个上传视频按钮
    //press 3=点击from library按钮
    if(_debug){
        $(document).on("keydown","html", function (e) {
            // console.log(e.which);
            // console.log($(this));
            // console.log(e);
            var tagName = e.target.tagName.toLowerCase();
            // console.log(tagName);

            if (tagName=='body') {
                switch (e.which-48){
                    case 1:{
                        jumpToCourseData();
                    }break;
                    case 2:{
                        $(".addSectionVideo").eq(0).click();
                    }break;
                    case 3:{
                        $("a.libraryico").eq(0).click();
                    }break;
                }
            }

        });
    }

});